#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <stdio_ext.h>
#include "../include/client.h"


//功能:客户端初始化
//参数:无
//返回值,成功返回套接字,创建套接字失败返回-1,connect 失败返回-2
//author:杨宁

int client_init(void)
{
	struct sockaddr_in servaddr;

	memset(&servaddr,0,sizeof(servaddr));

	servaddr.sin_family = PF_INET;
	servaddr.sin_port = htons(8888);
	servaddr.sin_addr.s_addr = inet_addr("127.0.0.2");

	
	//创建套接字
	int sockfd = socket(PF_INET,SOCK_STREAM,0);

	//创建客户端socket
	if (sockfd < 0)
	{
		perror("fail to socket");
		return -1;
	}

	//连接服务器
	if(connect(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
	{
        close(sockfd);
		perror("fail to connect");
		return -2;
	}

    puts("connect success");

    return sockfd;

}


//客户端界面
//author : 李超
void home_page(void)
{
    puts("* * * * * * * * 欢迎加入员工信息管理系统 * * * * * * * *");
    puts("*                                                      *");
    puts("*                   登入(l)                            *");
    puts("*                                                      *");
    puts("*                   退出(q)                            *");
    puts("* * * * * * * * * * * * * * * * * * * * * * * * * * * **");

}   
void user_page(void)
{
    puts("* * * * * * * * * 用户功能登录界面 * * * * * * * * * * *");
    puts("*                                                      *");
    puts("*                  查询(s)                             *");
    puts("*                  修改(c)                             *");
    puts("*                  退出(Q)                             *");
    puts("* * * * * * * * * * * * * * * * * * * * * * * * * * * **");
}
void admin_page(void)
{
    puts("* * * * * * * * * * 管理员功能选择 * * * * * * * * * * **");
    puts("*                   添加(a)                             *");
    puts("*                   删除(d)                             *");
    puts("*                   修改(C)                             *");
    puts("*                   查询(S)                             *");
    puts("*                   退出(Q)                             *");
    puts("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *");                                                   
}


//功能:发送请求并接受服务器回复
//参数:sockfd 套接字 msg 要发送的信息指针
//返回值: 服务器的回复消息
MSG_S *send_ask_recv_msg(int sockfd,MSG_C *msg)
{
	MSG_S *server_msg = (MSG_S *)malloc(sizeof(MSG_S));
	if(send(sockfd, msg, sizeof(MSG_C),0) < 0)
    {
        perror("fail to send msg_c");
        free(server_msg); //释放掉内存
        return NULL;
    }
    puts("-----------send success-----------");
    if(recv(sockfd,server_msg, sizeof(MSG_S),0) < 0)
    {
        perror("fail to recv msg_s");
        free(server_msg);//释放掉内存
        return NULL; 
    }
    puts("-----------recv success-----------");
	return server_msg;

}

//功能:登录请求处理
//参数:无
//返回值 1 表示普通登录成功, 2表示超级用户登录 -1 表示登录失败
int login_request_hand(int sockfd) //登录请求处理
{
    MSG_C msg_send;
    MSG_S *msg_recv = NULL;
    int flag = 0;

    bzero(&msg_send, sizeof(MSG_C)); //初始化为0

    puts("Please input your name:(len <= 64)");
    scanf("%s",msg_send.text.name);
    __fpurge(stdin);
    puts("Please input your password:");
    scanf("%s",msg_send.text.password);
    __fpurge(stdin);
    msg_send.type = 'l';

    msg_recv = send_ask_recv_msg(sockfd, &msg_send);

    //puts("--login send and recv success--- ");

    if(msg_recv != NULL)
    {
        switch(msg_recv->type)
        {
            case 'f':printf("Login failed :%s. You can relogin(input 'l'):\n", msg_recv->error); flag = 0;break;
            case 's':puts("Login success");flag = 1;break;
            default:puts("Login failed : recv message type is not right!"); flag = 0;break;
        }
        if(flag)
        {
            bzero(client_name, 64);
            strcpy(client_name, msg_send.text.name); // 登录成功后 初始化 client_name 
            //printf("recv flag = %d\n", msg_recv->text.flag);
            if(msg_recv->text.flag) 
            {
                flag = 2;
            }
            else
            {
                flag = 1;
            }
            return flag;
        }

        free(msg_recv);
    }
    else
    {
        puts("Login failed : recv message error");
    }
    return -1;


}

/*功能用户修改密码处理
 * 返回值 0 表示处理成功
 * 返回值 -1 表示处理失败
 * 入口参数 sockfd 为套接字*/


int user_chgpass_hand(int sockfd) //用户修改密码处理
{
    MSG_C msg_send;
    MSG_S *msg_recv = NULL;

    bzero(&msg_send, sizeof(MSG_C)); //初始化为0

    puts("Please input your new password:");
    scanf("%s", msg_send.text.password);
    __fpurge(stdin);

    strcpy(msg_send.text.name, client_name);
    msg_send.type = 'c';


    msg_recv = send_ask_recv_msg(sockfd, &msg_send);

    if(msg_recv != NULL)
    {
        switch(msg_recv->type)
        {
            case 'f':printf("change failed:%s\n", msg_recv->error);break;
            case 's':puts("change password success");break;
            default:puts("The type is not right!");break;
        }
        free(msg_recv);

        return 0;
    }
    else
    {
        puts("user request change password failed : send or recv message error!!");

        return -1;

    }
}


/*功能所有用户查询处理
 *参数: sockfd 套接字 flag 标志位 1 表示普通用户 2 表示超级用户
 *返回值 处理成功返回0 失败返回 -1

 */

int alluser_search_hand(int sockfd,int flag) //用户通用查询
{
    if(flag < 1 || flag > 2)
    {
        puts("fail to search hand: the flag is not exist");
        return -1;
    }

    MSG_C msg_send;
    MSG_S *msg_recv = NULL;
    bzero(&msg_send, sizeof(MSG_C));

    if(flag == 1)
    {
        msg_send.type = 's';
        strcpy(msg_send.text.name, client_name);
    }
    else
    {
        msg_send.type = 's';
        puts("Please input a name:");
        scanf("%s", msg_send.text.name);
        __fpurge(stdin);
    }
   
    msg_recv = send_ask_recv_msg(sockfd, &msg_send); //发送给服务器数据

    if(msg_recv != NULL)
    {

        switch(msg_recv->type)
        {
            case 's':
                {
                    puts("------------------------------------------------");

                    puts("name         id         password       addr     age    salay   flag ");

                    printf("%s     %s     %s         %s    %d    %.2f    %d\n", msg_recv->text.name, msg_recv->text.id,\
                            msg_recv->text.password, msg_recv->text.addr, msg_recv->text.age, msg_recv->text.salary,\
                            msg_recv->text.flag);
                    puts("------------------------------------------------");

                    free(msg_recv);

                    return 0;

                }

            case 'f':
                {

                    printf("fail to search staff message : %s\n", msg_recv->error);
                    free(msg_recv);

                    return -1;

                }
            default:
                {

                    free(msg_recv);
                    puts("fail to search staff message : the recv message type is not legal");

                    return -1;
                }
        }
    }
    else
    {
        puts("fail to search hand: send or recv message error");
        return -1;
    }
}


/* 功能:超级用户添加用户
 * 参数:sockfd 套接字
 * 返回值 -1 表示处理失败 0 表示处理成功
 */
int admin_adduser_hand(int sockfd) //超级用户添加用户
{
    MSG_C msg_send;
    MSG_S *msg_recv = NULL;
    bzero(&msg_send, sizeof(MSG_C));

    msg_send.type = 'a';

    //用户键入信息
    puts("Please input staff message:(Separated by spaces)");
    printf("ID (len = 6)/name(len < 64)/password(len < 64)/address(len < 64):");
    scanf("%s%s%s%s",msg_send.text.id, msg_send.text.name, msg_send.text.password, msg_send.text.addr);
    __fpurge(stdin);

    printf("id  : %s | name : %s | password : %s | address : %s\n",msg_send.text.id, msg_send.text.name, msg_send.text.password, msg_send.text.addr);
    printf("age/salary/flag:");
    scanf("%d%f%d", &msg_send.text.age, &msg_send.text.salary, &msg_send.text.flag);

    scanf("age : %d | salary : %f | flag : %d", &msg_send.text.age, &msg_send.text.salary, &msg_send.text.flag);
    __fpurge(stdin);


    msg_recv = send_ask_recv_msg(sockfd, &msg_send); //发送给服务器数据
    if(msg_recv != NULL)
    {
        switch(msg_recv->type)
        {
            case 's':
                {
                    puts("add user success");
                    free(msg_recv);
                    return 0;
                }
            case 'f':
                {

                    printf("add user failed : %s\n", msg_recv->error);
                    free(msg_recv);
                    return -1;
                }
            default:
                {
                    puts("add user failed : recv message typed is not legal");
                    free(msg_recv);
                    return -1;
                }
        }
    }
    else
    {
        puts("add user failed : recv or send message error");
        return -1;
    }


}

//超级用户修改信息 
//盘水养

int admin_chguser_msg_hand(int sockfd)
{
	MSG_C msg_send;

	MSG_S *msg_recv = NULL;

	bzero(&msg_send, sizeof(MSG_C)); //初始化为0

    puts("Please input meassage include -- id name password addr age salary flag -- (separated with space ):");

	scanf("%s%s%s%s%d%f%d", msg_send.text.id,msg_send.text.name,msg_send.text.password,msg_send.text.addr,\
			&msg_send.text.age,&msg_send.text.salary,&msg_send.text.flag);

	__fpurge(stdin);

	msg_send.type = 'C';


	msg_recv = send_ask_recv_msg(sockfd, &msg_send);

	if(msg_recv != NULL)
	{
		switch(msg_recv->type)
		{
			case 'f':printf("change failed:%s\n", msg_recv->error);
				 break;

			case 's':puts("change message success");
				 break;

			default:puts("The type is not right!");
				break;

		}
		free(msg_recv);

		return 0;
	}

	else
	{
		puts("user request change failed : send or recv message error!!");

		return -1;

	}
}

//宋亚武 超级用户删除功能
int admin_deluser_hand(int sockfd)
{
	MSG_C msg_send;
	MSG_S *msg_recv = NULL;

	bzero(&msg_send,sizeof(MSG_C)); //初始化msg_send
	puts("Please input delete name:");
	scanf("%s",msg_send.text.name);
	__fpurge(stdin); //清除输入流的缓存

	msg_send.type = 'd';

	msg_recv = send_ask_recv_msg(sockfd,&msg_send);

	if(msg_recv != NULL)
	{
		switch(msg_recv -> type)
		{
			case 'f':printf("delete failed:%s\n",msg_recv -> error);
					break;
			case 's':puts("delete success");
					break;
			default:puts("The type is not right!");
					break;
		}
		free(msg_recv);

		return 0;
	}
	else
	{
		puts("User request delete failed:send or recv message error!");

		return -1;
	}
}

int client_exit(int sockfd) //客户端退出
{
    MSG_C msg_send;
    bzero(&msg_send, sizeof(MSG_C));
    msg_send.type = 'q';

    if(send(sockfd, &msg_send, sizeof(MSG_C), 0) < 0)
    {
        perror("fail to send");
        return -1;
    }

    close(sockfd);

    
    return 0;


}



